
#include "air780e_common.h"


 

extern int pika_pvsprintf(char** buff, const char* fmt, va_list args);
void pika_platform_printf(char* fmt, ...) {
    
    #if 1
    va_list args;
    va_start(args, fmt);

    // luat_debug_print("fmt_addr:%p, args_addr:%p", fmt, args);
    // luat_debug_print("args - &fmt = %p", (char*)args - fmt);
    char* buff = NULL;
    int required_size = pika_pvsprintf(&buff, fmt, args);
    va_end(args);
    if (required_size < 0) {
        goto __exit;
    }
    //luat_debug_print自带换行，所以要手动消除一个回车换行
    if (buff[required_size-1] == '\n'){
        buff[required_size-1] = '\0';
    }
    if (buff[required_size-2] == '\r'){
        buff[required_size-2] = '\0';
    }
    luat_debug_print(buff);
__exit:
    if (NULL != buff) {
        pika_platform_free(buff);
    }
    #endif
    

    #if 0
    size_t fmt_len = strGetSize(fmt);
    luat_debug_print("fmt_len:%d", fmt_len);
    for(int i=0; i < fmt_len; i++){
        luat_debug_print("%c,%X,", fmt[i], fmt[i]);
    }
    luat_debug_print("-1 %c,%X,", fmt[fmt_len-1], fmt[fmt_len-1]);
    luat_debug_print("-2 %c,%X,", fmt[fmt_len-2], fmt[fmt_len-2]);
    if (fmt[fmt_len-1] == '\n'){
        fmt[fmt_len-1] = '\0';
    }
    if (fmt[fmt_len-2] == '\r'){
        fmt[fmt_len-2] = '\0';
    }
	va_list ap;
	va_start(ap, fmt);
	soc_vsprintf(0, fmt, ap);
	va_end(ap);
    #endif
}


// 四舍五入函数
double round_to_precision(double value, int precision) {
    double factor = pow(10.0, precision);
    return round(value * factor) / factor;
}
#if 1
// 将浮点数转换为字符串
char* float_to_string(double value, int precision) {
    // 四舍五入到指定精度
    value = round_to_precision(value, precision);

    char *buffer = (char *)pikaMalloc(FTOA_MAX_LENGTH);
    if (buffer == NULL) {
        return NULL; // 内存分配失败
    }
    
    // 处理负数
    int is_negative = 0;
    if (value < 0.0) {
        is_negative = 1;
        value = -value;
    }

    // 提取整数部分
    long integer_part = (long)value;
    double fractional_part = value - integer_part;
    // 将整数部分转换为字符串
    int length = pika_sprintf(buffer, "%ld", integer_part);
    
    // 添加小数点
    if (precision > 0) {
        buffer[length++] = '.';
        buffer[length] = '\0';
    }
    // 将小数部分转换为字符串
    for (int i = 0; i < precision; ++i) {
        fractional_part *= 10.0;
        int digit = (int)round(fractional_part);
        fractional_part -= digit;
        buffer[length++] = '0' + digit;
    }
    buffer[length] = '\0';

    // 添加负号（如果需要）
    if (is_negative) {
        pika_platform_memmove(buffer + 1, buffer, length + 1); // 移动字符串
        buffer[0] = '-';
    }
    return buffer;
}
#endif
#if 0
// 将浮点数转换为字符串
char* float_to_string(double value, int precision) {
    // 四舍五入到指定精度
    value = round_to_precision(value, precision);

    char *buffer = (char *)pikaMalloc(MAX_LENGTH * sizeof(char));
    if (buffer == NULL) {
        return NULL; // 内存分配失败
    }

    // 使用 snprintf 直接格式化浮点数
    pika_snprintf(buffer, MAX_LENGTH, "%.*f", precision, value);

    return buffer;
}
#endif

void pika_platform_disable_irq_handle(void) {
    taskENTER_CRITICAL();
}

void pika_platform_enable_irq_handle(void) {
    taskEXIT_CRITICAL();
}

void* pika_user_malloc(size_t size) {
    return pvPortMalloc(size);
}

void pika_user_free(void* ptr, size_t size) {
    vPortFree(ptr);
}


int air780e_pin_name_to_id(char* pin) {
    // Args* buffs = New_strBuff();
    uint16_t gpioPin = 0;
    //WAKEUP0、WAKEUP1、WAKEUP2、PWRKEY

    if (pin[0] == 'W'){
        if (!strncmp(pin, "WAKEUP", sizeof("WAKEUP")-1)){
            switch(pin[sizeof("WAKEUP") - 1]){
                case '0':
                    gpioPin = HAL_WAKEUP_0;
                    break;
                case '1':
                    gpioPin = HAL_WAKEUP_1;
                    break;
                case '2':
                    gpioPin = HAL_WAKEUP_2;
                    break;
                default:
                    gpioPin = -1;
            }
        }
    }
    else if (pin[0] == 'P'){
        if(!strncmp(pin, "PWRKEY", sizeof("PWRKEY")-1)){
            gpioPin = HAL_WAKEUP_PWRKEY;
        }
        else{
            gpioPin = -1;
        }
    }
    else{
        gpioPin = fast_atoi(pin);
    }

    if (gpioPin > HAL_GPIO_QTY){
        return -1;
    }
    return gpioPin;
}

uint32_t getPinMode(char* mode) {
    if (strEqu(mode, "out")) {
        return GPIO_DIRECTION_OUTPUT;
    }
    if (strEqu(mode, "in")) {
        return GPIO_DIRECTION_INPUT;
    }
    return NULL;
}


uint8_t GPIO_enable_clock(char* pin) {
    GPIO_driverInit();
    return 0;
}

uint8_t GPIO_disable_clock(char* pin) {
    GPIO_driverDeInit();
    return 0;
}

void pika_platform_reboot(void) {
    ResetECSystemReset(); // 重启系统
}


